/******************************************************************************/
/* RETARGET.C: 'Retarget' layer for target-dependent low level functions      */
/******************************************************************************/
/* This file is part of the uVision/ARM development tools.                    */
/* Copyright (c) 2005 Keil Software. All rights reserved.                     */
/* This software may only be used under the terms of a valid, current,        */
/* end user licence from KEIL for a compatible version of KEIL software       */
/* development tools. Nothing else gives you the right to use this software.  */
/******************************************************************************/

#include <stdio.h>
#include <time.h>
#include <rt_misc.h>
#include "MyIncludes.h"


#pragma import(__use_no_semihosting_swi)


extern int  sendchar(u8 ch);  /* in Serial.c */
extern int  getkey(void);      /* in Serial.c */
extern long timeval;           /* in Time.c   */


struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;


int fputc(int ch, FILE *f) {
  return (sendchar(ch));
}

int fgetc(FILE *f) {
  return (sendchar(getkey()));
}


int ferror(FILE *f) {
  /* Your implementation of ferror */
  return EOF;
}


void _ttywrch(int ch) {
//  sendchar (ch);
}


void _sys_exit(int return_code) {
  while (1);    /* endless loop */
}


int  getkey(void)  { 
  while (!(USART1->SR & USART_FLAG_RXNE)); 
  return ((int)(USART1->DR & 0x1FF)); 
} 
/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
int  sendchar(u8 ch)
{
//	MidPutNBytesToUart1TxBuff(&ch,1);
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (uint8_t) ch);
  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {}
  return ch;
}

/*----------------------------------------------------------------------------
 *      RL-ARM - FlashFS
 *----------------------------------------------------------------------------
 *      Name:    RETARGET.C 
 *      Purpose: Retarget low level functions
 *      Rev.:    V4.20
 *----------------------------------------------------------------------------
 *      This code is part of the RealView Run-Time Library.
 *      Copyright (c) 2004-2011 KEIL - An ARM Company. All rights reserved.
 *---------------------------------------------------------------------------*/

// #include <stdio.h>
// #include <string.h>
// #include <rt_sys.h>
// #include <File_Config.h>

// #pragma import(__use_no_semihosting_swi)
// #define STDIO
// /* The following macro definitions may be used to translate this file:

//   STDIO - use standard Input/Output device
//           (default is NOT used)
//  */

// /* Standard IO device handles. */
// #define STDIN   0x8001
// #define STDOUT  0x8002
// #define STDERR  0x8003

// /* Standard IO device name defines. */
// const char __stdin_name[]  = "STDIN";
// const char __stdout_name[] = "STDOUT";
// const char __stderr_name[] = "STDERR";

// struct __FILE { int handle; /* Add whatever you need here */ };

// #ifdef STDIO
//  extern int sendchar (int ch);
//  extern int getkey (void);
// #endif


// int fputc(int ch, FILE *f) {
//   return (sendchar(ch));
// }

// int fgetc(FILE *f) {
//   return (sendchar(getkey()));
// }

// /*--------------------------- _ttywrch --------------------------------------*/

// void _ttywrch (int ch) {
// #ifdef STDIO
//   sendchar(ch);
// #endif
// }

// /*--------------------------- _sys_open -------------------------------------*/

// FILEHANDLE _sys_open (const char *name, int openmode) {
//   /* Register standard Input Output devices. */
//   if (strcmp(name, "STDIN") == 0) {
//     return (STDIN);
//   }
//   if (strcmp(name, "STDOUT") == 0) {
//     return (STDOUT);
//   }
//   if (strcmp(name, "STDERR") == 0) {
//     return (STDERR);
//   }
//   return (__sys_open (name, openmode));
// }

// /*--------------------------- _sys_close ------------------------------------*/

// int _sys_close (FILEHANDLE fh) {
//   if (fh > 0x8000) {
//     return (0);
//   }
//   return (__sys_close (fh));
// }

// /*--------------------------- _sys_write ------------------------------------*/

// int _sys_write (FILEHANDLE fh, const U8 *buf, U32 len, int mode) {
// #ifdef STDIO
//   if (fh == STDOUT) {
//     /* Standard Output device. */
//     for (  ; len; len--) {
//       sendchar (*buf++);
//     }
//     return (0);
//   }
// #endif
//   if (fh > 0x8000) {
//     return (-1);
//   }
//   return (__sys_write (fh, buf, len));
// }

// /*--------------------------- _sys_read -------------------------------------*/

// int _sys_read (FILEHANDLE fh, U8 *buf, U32 len, int mode) {
// #ifdef STDIO
//   if (fh == STDIN) {
//     /* Standard Input device. */
//     for (  ; len; len--) {
//       *buf++ = getkey ();
//     }
//     return (0);
//   }
// #endif
//   if (fh > 0x8000) {
//     return (-1);
//   }
//   return (__sys_read (fh, buf, len));
// }

// /*--------------------------- _sys_istty ------------------------------------*/

// int _sys_istty (FILEHANDLE fh) {
//   if (fh > 0x8000) {
//     return (1);
//   }
//   return (0);
// }

// /*--------------------------- _sys_seek -------------------------------------*/

// int _sys_seek (FILEHANDLE fh, long pos) {
//   if (fh > 0x8000) {
//     return (-1);
//   }
//   return (__sys_seek (fh, pos));
// }

// /*--------------------------- _sys_ensure -----------------------------------*/

// int _sys_ensure (FILEHANDLE fh) {
//   if (fh > 0x8000) {
//     return (-1);
//   }
//   return (__sys_ensure (fh));
// }

// /*--------------------------- _sys_flen -------------------------------------*/

// long _sys_flen (FILEHANDLE fh) {
//   if (fh > 0x8000) {
//     return (0);
//   }
//   return (__sys_flen (fh));
// }

// /*--------------------------- _sys_tmpnam -----------------------------------*/

// int _sys_tmpnam (char *name, int sig, unsigned maxlen) {
//   return (1);
// }

// /*--------------------------- _sys_command_string ---------------------------*/

// char *_sys_command_string (char *cmd, int len) {
//   return (cmd);
// }

// /*--------------------------- _sys_exit -------------------------------------*/

// void _sys_exit (int return_code) {
//   /* Endless loop. */
//   while (1);
// }

/*----------------------------------------------------------------------------
 * end of file
 *---------------------------------------------------------------------------*/



